<template>
	<view class="docyy">
		<!-- 头部 -->
		<view class="doc_title">
			<image class="doc_portrait" :src="dataobj.img?http_img+dataobj.img:''"></image>
			<view class="title_info">
				<view class="font18 font-323232 font-b">{{dataobj.name}}</view>
				<view class="font14 font-989898">
					{{dataobj.academic_title_name.name?dataobj.academic_title_name.name:'暂无简介'}}
				</view>
			</view>
			<!-- <view class="title_follow" @click="Onaddfollow">
				{{dataobj.is_follow == 0 ?'+关注':'已关注'}}
			</view> -->
		</view>
		<!-- 边框 -->
		<view class="com_border"></view>
		<!-- 日期 -->
		<view class="time_title font13 font-989898">已选日期 :
			<text class="font-323232 font-b">{{day}} {{week}}</text>
		</view>
		<!-- 日期列表 -->
		<view class="time_bos">
			<view class="time_list">
				<view v-for="(item,index) in timeList" :key="index" class="time_listbos" @click="Ontime(item)">
					<view class="time_listbox flex-column-center"
						:class="item.show?'background_6bcbb6':'background_ffffff'">
						<view :class="item.show?'font-ffffff':'font-989898'" class="font11">{{item.weekStr}}
						</view>
						<view :class="item.show?'font-ffffff':'font-6bcbb6'" class="font13">{{item.nameStr}}
						</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 数据 -->
		<view class="docyy_data">
			<view class="docyy_list font13 flex-row-between" v-for="(item,index) in doctime" :key="index"
				@click="OnPopupshow(item)">
				<view class="list_bos">
					<view class="font-323232">{{item.ZAA07_1}}~{{item.ZAA08_1}}</view>
					<view class="flex-row-start margin">
						<view class="AVAIlABLECOUNT">余号{{item.AVAIlABLECOUNT}}</view>
						<text class="font-ef9140">￥{{item.BBY25}}</text>
					</view>
				</view>
				<image src="../../../static/my/morea.png"></image>
			</view>
		</view>
		<!-- 预约弹窗 -->
		<uni-popup ref="popup" type="bottom">
			<view class="ComPopup_bos">
				<view class="popup_title">
					<view class="popup_time font15 font-323232 font-b">预约</view>
					<image @click="Onclose" class="popup_img" src="../../../static/close.png"></image>
				</view>
				<view class="ComPopup_title flex-row-center">
					<image class="ComPopup_image" :src="dataobj.img?http_img+dataobj.img:''"></image>
					<view class="ComPopup_titlebos">
						<view class="ComPopup_titlebox">
							<view class="font-32323 font14 font-b">医师:</view>
							<view class="ComPopup_info">
								<view class="font-989898 font14">{{dataobj.name}}</view>
							</view>
						</view>
						<view class="ComPopup_titlebox">
							<view class="font-32323 font14 font-b">科室:</view>
							<view class="ComPopup_info">
								<view class="font-989898 font14">{{dataobj.room_name?dataobj.room_name:'暂无'}}</view>
							</view>
						</view>
						<view class="ComPopup_titlebox">
							<view class="font-32323 font14 font-b">费用:</view>
							<view class="ComPopup_info">
								<view class="font-989898 font14"><text>￥{{docobj.BBY25}}</text></view>
							</view>
						</view>
						<view class="ComPopup_titlebox">
							<view class="font-32323 font14 font-b">时段:</view>
							<view class="ComPopup_info">
								<view class="font-989898 font14">{{day}} {{week}}</view>
								<view class="font-989898 font14">开始时间:{{docobj.ZAA07_1}}</view>
								<view class="font-989898 font14">结束时间:{{docobj.ZAA08_1}}</view>
							</view>
						</view>
					</view>
				</view>
				<view class="ComPopup_choois font-32323 font14 font-b">请选择就诊人</view>
				<view class="ComPopup_chooisa font-989898 font14">
					({{familyobj.the_patient_info.name?familyobj.the_patient_info.name+'卡号:'+familyobj.the_patient_info.code:'暂无'}})
				</view>
				<view class="ComPopup_list">
					<view class="ComPopup_listname font14 flex-row-center"
						:class="item.id==familyobj.id?'listname_a':'listname_b'" v-for="(item,index) in familyList"
						:key="index" @click="Onchooisfamily(item)">
						{{item.the_patient_info.namea}}
					</view>
					<view class="ComPopup_listadd flex-row-center" @click="Onjzrgladd">+</view>
					<!-- <view class="ComPopup_listadd" v-if="familyList.length==0">+</view> -->
				</view>
				<view class="popup_determine font-ffffff font15" @click="Onconfirm">确认挂号</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				id: null,
				http_img: '', //前缀
				timeList: [], //时间
				day: '', //日期
				dataobj: {}, //医生详情
				week: '', //周几
				doctime: [], //医生排版时间
				docobj: {}, //选择的时间段
				familyList: [], //就诊人列表
				familyobj: {}, //默认就诊人
			}
		},
		onLoad: function(options) {
			this.id = options.id
			if (options.day) {
				this.day = options.day
			} else {
				let date = new Date();
				let years = date.getFullYear()
				let months = date.getMonth() + 1
				if (months < 10) {
					months = "0" + months
				}
				let days = date.getDate()
				if (days < 10) {
					days = "0" + days
				}
				this.day = years + '-' + months + '-' + days
			}

			this.allWeek()
			//获取就诊人
			this.OnPatient()
			this.http_img = this.$store.state.http_img
		},
		methods: {
			//获取就诊人
			OnPatient() {
				this.$Http({
					url: 'listThePatient',
					data: {}
				}).then(res => {
					if (res.code == 1) {
						if (!res.data || res.data.length == 0) {} else {
							this.familyList = res.data
							this.familyobj = res.data[0] ? res.data[0] : {}
							res.data.forEach(item => {
								item.the_patient_info.namea = item.the_patient_info.name.substring(item
									.the_patient_info.name.length - 2)
								if (item.isdefault == 1) {
									this.familyobj = item
								}
							})
						}
					}
				})
			},
			//选择就诊人
			Onchooisfamily(e) {
				this.familyobj = e
			},
			//添加就诊人
			Onjzrgladd() {
				this.$store.commit('Onjzkurl', '/pagesA/his/docyy/docyy?id=' + this.id + '&day=' + this.day)
				uni.navigateTo({
					url: '/pagesA/my/jzrgladd',
				})
			},
			//获取时间
			allWeek(dateTime) {
				var me = this;
				var sevenDays = [];
				var time = new Date().getTime();
				var dateStrList = [];
				for (var i = 0; i < 7; i++) {
					var date = new Date(time + i * 24 * 3600 * 1000);
					var weekTime = me.formatDate(date);
					var splitArr = weekTime.split(" ");
					var dateStr = splitArr[0];
					var weekStr = splitArr[1];
					var nameStr = splitArr[2];
					var dateObj = {};
					dateObj.dateStr = dateStr;
					dateObj.weekStr = weekStr;
					dateObj.nameStr = nameStr;
					sevenDays.push(dateObj);
				}
				this.timeList = sevenDays
				this.timeList.forEach(item => {
					item.show = false
					if (item.dateStr == this.day) {
						item.show = true
						this.week = item.weekStr
					}
				})
				this.Oninfo()
			},
			formatDate(date) {
				var year = date.getFullYear();
				var month = (date.getMonth() + 1);
				month = month <= 9 ? '0' + month : month;
				var day = date.getDate();
				day = day <= 9 ? '0' + day : day;
				var datetime = year + '-' + month + '-' + day;
				var datename = day + '号';
				var week = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'][date.getDay()];
				return datetime + ' ' + week + ' ' + datename;
			},
			//详情
			Oninfo() {
				this.$Http({
					url: 'getDataDoctorList',
					data: {
						doctor_his_id: this.id,
						date: this.day
					}
				}).then(res => {
					if (res.data.length > 0) {
						res.data.forEach(item => {
							item.ZAA07_1 = item.ZAA07.substr(-8)
							item.ZAA08_1 = item.ZAA08.substr(-8)
						})
						this.dataobj = res.data[0].doctor_info
						this.doctime = res.data
					}
				})
			},
			//选择时间
			Ontime(e) {
				this.timeList.forEach(item => {
					item.show = false
					if (e.dateStr == item.dateStr) {
						item.show = true
					}
				})
				this.day = e.dateStr
				this.week = e.weekStr
				this.Oninfo()
			},
			//预约弹窗
			OnPopupshow(e) {
				if (e.AVAIlABLECOUNT < 0) {
					return
				}
				this.docobj = e
				this.$refs.popup.open()
			},
			//关闭弹窗
			Onclose() {
				this.docobj = {}
				this.$refs.popup.close()
			},
			//添加关注
			Onaddfollow() {
				let url = ''
				if (this.dataobj.is_follow == 0) {
					url = 'addUserDoctor'
				} else {
					url = 'deladdUserDoctor'
				}
				this.$Http({
					url: url,
					data: {
						doctor_id: this.id,
					},
					show: true,
					type: 'POST'
				}).then(res => {
					if (res.code == 1) {
						if (this.dataobj.is_follow == 1) {
							this.dataobj.is_follow = 0
						} else {
							this.dataobj.is_follow = 1
						}
					}
				})
			},
			//确认挂号
			Onconfirm() {
				let data = {
					begin_time: this.docobj.ZAA07,
					end_time: this.docobj.ZAA08,
					money: this.docobj.BBY25,
					doctor_his_id: this.id,
					the_patient_id: this.familyobj.the_patient_info.id
				}
				this.$Http({
					url: 'createHunmnumberOrder',
					data: data,
					type: 'POST',
					show: true
				}).then(res => {
					if (res.code = 1) {
						this.$refs.popup.close()
						uni.navigateTo({
							url: '/pagesA/register/conpay?id=' + res.data.order_code,
						});
					}
				})
			},
		}
	}
</script>

<style lang="less">
	@import url("style.less");
</style>
